/* © 2007 Adobe Systems Incorporated. All rights reserved. */
package com.shubhjai.musiclib.data.parsers
{
	import flash.events.EventDispatcher;
	import com.shubhjai.musiclib.commands.Command;
	import com.shubhjai.musiclib.model.Record;
	
	/**
	 * This class is a implementation of an employee data parser for the DSML XML schema,
	 * commonly used as an output format for LDAP applications.
	 */
	public class RecordDSMLSchemaParser implements IDataParser
	{
		
		/** 
		 * Parse the given data (assumed to be CSV, line delimited data.)
		 * 
		 * @param data The data object that will be parsed. 
		 * @return An array of Record objects.
		 */
		public function parse( data:Object ) : Array
		{
			//trace("dsml parse");
			var results : Array = new Array();
			
			var employee : Record;
			
			var xml : XML = XML( data );
			
			var entries : XMLList = xml  .. *::entry;
			var attName : String;
			var value : String;
			for each ( var entryXML:XML in entries )
			{	
				employee = new Record();

				// parse the id
				employee.id = getIdFromDN( entryXML.attribute("dn"), "," );
				employee.email = employee.id + "@employeecorp.com";
				
				var ind : int;
				for each ( var attrXML : XML in entryXML.children() )
		  		{
		  			attName = attrXML.attribute( "name" );
		  			if ( attrXML.children().length() == 0 )
		  			{
		  				value = null;
		  			}
		  			else
		  			{
			  			value = attrXML.children()[0].toString();
			  		}
			  		
			  	

		  			switch ( attName )
		  			{
		  				case "givenName":
		  					employee.firstName = value;
		  					break;
		  				case "sn":
		  					employee.lastName = value;
		  					break;
		  				case "displayName":
		  					employee.displayName = value
		  					break;
		  				case "title":
		  					employee.title = value;
		  					break;
		  				case "department":
		  					employee.department = value;
		  					break;
		  				case "manager":
		  					employee.managerId = getIdFromDN( value, " " );
		  					break;
		  				case "telephoneNumber":
		  					employee.phone = value;
		  					break;
		  				case "mobile":
		  					employee.cellPhone = value;
		  					break;
		  				case "directDial":
		  					employee.phoneExtension = value;
		  					break;
		  				case "PhysicalDeliveryOfficeName":
		  					employee.deskLocation = value;
		  					break;
		  				case "location":
		  					
		  					if ( value != null )
		  					{
		  						ind = value.indexOf("-");
		  						if ( ind > 0 )
		  						{
		  							value = value.substring( ind + 1 );
		  						}
		  						employee.location = value;
		  					}
		  					
		  					break;
		  				case "l":
		  					employee.city = value;
		  					break;
		  				case "st":
		  					employee.state = value;
		  					break;
		  				case "c":
		  					employee.countryCode = value;
		  					break;
		  				case "postalCode":
		  					employee.postalCode = value;
		  					break;
		  					
		  				default: 
		  					break;
		  			}
		  		}
		  		
				results.push( employee );
			}

			xml = null;
			entries = null;
			return results;
			
		}
		
		/**
		 * Parse an id out of a distinguished name value.
		 * @param dn The distiguished name to parse e.g. CN=username,CN=Users,DC=employeenet,DC=global,DC=employee,DC=com'
		 * @param separatorChar The separator char in the DN (e.g. ' ' or ',') 
		 * @return The user id from the DN.
		 */ 
		private function getIdFromDN( dn:String, separatorChar:String )  : String
		{
			return dn.split( separatorChar )[0].substring(3);
		}
	}
}